package io.renren.modules.sys.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.service.entity.ProductEntity;
import io.renren.modules.service.entity.UserEntity;
import io.renren.modules.service.service.ProductService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 产品表
 */
@RestController
@RequestMapping("sys/product")
public class SysProductController {
    @Autowired
    private ProductService productService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:product:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = productService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:product:info")
    public R info(@PathVariable("id") Integer id) {
        ProductEntity product = productService.getById(id);

        return R.ok().put("product", product);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:product:save")
    public R save(@RequestBody ProductEntity product) {
        productService.save(product);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:product:update")
    public R update(@RequestBody ProductEntity product) {
        productService.updateById(product);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除产品")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:product:delete")
    public R delete(@RequestBody Integer[] ids) {
        productService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @RequiresPermissions("sys:product:export")
    public void export(ProductEntity product, HttpServletResponse response) {
        List<ProductEntity> list = productService.list(new QueryWrapper<ProductEntity>()
                .eq(StringUtils.isNotBlank(product.getName()), "name", product.getName())
                .eq(StringUtils.isNotBlank(product.getNumber()), "number", product.getNumber())
                .eq(StringUtils.isNotBlank(product.getWorkerName()), "worker_name", product.getWorkerName())
                .eq(StringUtils.isNotBlank(product.getCurrent()), "current", product.getCurrent())
                .orderByDesc("id")
        );
        String codedFileName = "产品列表.xls";
        Workbook sheets = ExcelExportUtil.exportExcel(new ExportParams(null, "产品列表"), ProductEntity.class, list);

        try {
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(codedFileName, "utf-8"));
            ServletOutputStream out = response.getOutputStream();
            sheets.write(out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
